//
//  AddContactViewController.m
//  Yihubang
//
//  Created by hyu on 16/8/8.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "AddContactViewController.h"
#import <AddressBookUI/AddressBookUI.h>
@interface AddContactViewController ()
@property (weak  , nonatomic)  IBOutlet UITextField         *relationText;
@property (weak  , nonatomic)  IBOutlet UITextField         *contactNametext;
@property (weak  , nonatomic)  IBOutlet UITextField         *telNumtext;
@property (weak  , nonatomic)  IBOutlet UIButton            *addContact_button;
@property (weak  , nonatomic)  IBOutlet UIButton            *Complete_button;
@property (strong, nonatomic)           NSMutableDictionary *data;
@property (assign, nonatomic)           BOOL                 edit;



@end

@implementation AddContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.telNumtext.maxLenght= 11;
    self.navigationItem.title = @"添加紧急联系人";
//    RAC(self.Complete_button, enabled) = [[RACSignal
//                                          combineLatest:@[ RACObserve(self.contactNametext, text), RACObserve(self.telNumtext,text)]
//                                          reduce:^(NSString *name, NSString * phone) {
//                                              return @(name.length >0 && phone.length ==11);
//                                          }] distinctUntilChanged];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:self.contactNametext];
    if([[self.params allKeys] containsObject:@"info"]){
        self.data =self.params[@"info"];
        self.edit =YES;
        self.telNumtext.text = self.data[@"phone"];
        self.contactNametext.text =self.data[@"name"];
        self.relationText.text = self.data[@"relation"];
    }
    [self.addContact_button addTarget:self action:@selector(addContactFromAddressBook) forControlEvents:UIControlEventTouchUpInside];
    [self.Complete_button addTarget:self action:@selector(addContact:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(NSMutableDictionary *)data{
    if(!_data){
        _data =[NSMutableDictionary dictionary];
    }
    return _data;
}
-(void)addContactFromAddressBook{
    [LYAuthorizedMaster AddressBookAuthorityCheckSuccess:^(void){
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        UIViewController *vct = [story instantiateViewControllerWithIdentifier:@"MyAdressContractsVC"];
        [vct setCallbackBlock:^(id callbackData){
            self.telNumtext.text = callbackData[@"personPhones"][0];
            self.contactNametext.text = callbackData[@"personName"];
        }];
        [self.navigationController pushViewController:vct animated:YES];
    } fail:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)addContact:(id)sender{
    if(self.contactNametext.text.length == 0){
        [self hiddenHUDWithString:@"请填写姓名" error:NO];
        return;
    }
    if(self.telNumtext.text.length != 11){
        [self hiddenHUDWithString:@"请填写正确的手机号" error:NO];
        return;
    }
    if(self.relationText.text.length == 0){
        [self hiddenHUDWithString:@"请填写联系人与你的关系" error:NO];
        return;
    }

    [self showHUDWithString:@"提交中..."];
    if(self.edit){
        [self editThisContact];
    }else{
        [self addThisContact];
    }
}
-(void)addThisContact{
     @weakify(self)
    [[[HttpManagerCenter sharedHttpManager] addContact:self.contactNametext.text phone:self.telNumtext.text friendName:nil nikeName:self.relationText.text resultClass:nil] subscribeNext:^(BaseModel *model){
        @strongify(self)
        if(model.code == 200){
            [self callback];
            [self hiddenHUDWithString:model.message error:NO];
        }else{
            [self hiddenHUDWithString:model.message error:NO];
        }
    } error:^(NSError *error) {
    } completed:^{
    }];
}
-(void)editThisContact{
    @weakify(self)
    [[[HttpManagerCenter sharedHttpManager] editContact:self.contactNametext.text phone:self.telNumtext.text friendName:nil nikeName:self.relationText.text byId:self.data[@"cid"]  resultClass:nil] subscribeNext:^(BaseModel *model){
        @strongify(self)
        if(model.code == 200){
            [self callback];
            [self hiddenHUDWithString:model.message error:NO];
        }else{
            [self hiddenHUDWithString:model.message error:NO];
        }
    } error:^(NSError *error) {
    } completed:^{
    }];
}
-(void)deleteThisContact{
    if([self.params[@"count"] integerValue] >1){
        [[[HttpManagerCenter sharedHttpManager] deleteContact:self.data[@"cid"] resultClass:nil] subscribeNext:^(BaseModel *model){
            if(model.code ==200){
                [self callback];
            }
        }];
    }else{
         [self hiddenHUDWithString:@"至少需要一位紧急联系人" error:NO];
    }
}
-(void)callback{
    if (self.callbackBlock) {
        self.callbackBlock(@"qq");
        [self gotoBack];
    }
}

//-(void)getRelation{
//    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
//    UIViewController *vct = [story instantiateViewControllerWithIdentifier:@"RelationViewController"];
//    if(self.data.count >0){
//        vct.params= @{@"relation":self.data[@"relation"]};
//    }
//     @weakify(self)
//    [vct setCallbackBlock:^(id callBackData) {
//         @strongify(self)
//        [self.relation_Button setTitle:callBackData forState:UIControlStateNormal];
//        [self.data setObject:callBackData forKey:@"relation"];
//    }];
//    [self pushViewController:vct animated:YES];
//
//}
@end
